package co.fitstart.admin.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.BusinessException;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;


/**
 * 
 * @author jiecheng
 */
@Controller
@RequestMapping("/sites")
@MenuSetting("menu-system")
public class SiteController {
    
    private static final Logger logger = LoggerFactory.getLogger(SiteController.class);

    @Autowired
    private SiteService siteService;
    
    @Autowired
    private PrincipalService principalService;
    
    /**
     * list all sites 
     * 
     * @param login
     * @return 
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-site-management")
    public ModelAndView list(@Secure Login login){
        
        ModelAndView modelAndView = new ModelAndView("system/site/site-list");
        
        List<Site> sites = new ArrayList<Site>();
        
        if(login.hasSite())
            sites.add(siteService.load(login.getSite().getId()));
        else if(login.isAreaManager() || login.isAreaServer()) {
            Principal principal = principalService.load(Principal.class, login.getId());
            
            sites = siteService.listSite(principal);
        } else
            sites = siteService.loadAll();
        
        modelAndView.addObject("sites", sites);
        
        return modelAndView;
    }
    
    /**
     * Display site view panel
     * 
     * @param siteId
     * @return
     */
    @RequestMapping(value="/{siteId}", method=RequestMethod.GET)
    @MenuSetting("submenu-site-management")
    public ModelAndView view(@PathVariable("siteId") Long siteId){
        
        ModelAndView modelAndView = new ModelAndView("system/site/site-view");
        
        Site site = siteService.load(siteId);
        
        modelAndView.addObject("site", site);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param siteId
     * @return
     */
    @RequestMapping(value = "/{siteId}/business-hour", method = RequestMethod.GET)
    public ModelAndView businessHour(@PathVariable("siteId") Long siteId) {
        
        ModelAndView modelAndView = new ModelAndView("system/site/site-business-hour-view");
        
        Site site = siteService.load(siteId);
        
        modelAndView.addObject("site", site);
        
        return modelAndView;
    }
    
    /**
     * Display edit panel
     * 
     * @param siteId
     * @return
     */
    @RequestMapping(value = "/{siteId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("siteId") Long siteId){
    
        ModelAndView modelAndView = new ModelAndView("system/site/site-edit");
        
        Site site = siteService.load(siteId);

        modelAndView.addObject(site);
        
        return modelAndView;
        
    }

    /**
     * Do edit site 
     * 
     * @param site
     * @return
     */
    @RequestMapping(value = "/{siteId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("siteId") Site site){
        
        Result result = new Result();
     
        try{
            
            siteService.edit(site);
            
            result.success().message("修改门店成功");
         
        }catch(Exception e){
            logger.error("onEdit() - ", e);
            result.fail().message("修改门店失败");
             
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    
    /**
     * Display edit roomlimit panel
     * 
     * @param siteId
     * @return
     */
    @RequestMapping(value = "/{siteId}/edit-roomlimit", method = RequestMethod.GET)
    public ModelAndView editRoomLimit(@PathVariable("siteId") Long siteId){
    
        ModelAndView modelAndView = new ModelAndView("system/site/site-edit-roomlimit");
        
        Site site = siteService.load(siteId);

        modelAndView.addObject(site);
        
        return modelAndView;
        
    }

    /**
     * Do edit roomlimit 
     * 
     * @param site
     * @return
     */
    @RequestMapping(value = "/{siteId}/edit-roomlimit", method = RequestMethod.PATCH)
    public @ResponseBody String onEditRoomLimit(@Preloading("siteId") Site site){
        
        Result result = new Result();
     
        try{
            
            siteService.edit(site);
            
            result.success().message("修改门店成功");
         
        }catch(Exception e){
            logger.error("onEditRoomLimit() - ", e);
            result.fail().message("修改门店失败");
             
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display edit roomlimit panel
     * 
     * @param siteId
     * @return
     */
    @RequestMapping(value = "/{siteId}/edit-room-description", method = RequestMethod.GET)
    public ModelAndView editRoomDescription(@PathVariable("siteId") Long siteId){
    
        ModelAndView modelAndView = new ModelAndView("system/site/site-edit-room-description");
        
        Site site = siteService.load(siteId);

        modelAndView.addObject(site);
        
        return modelAndView;
        
    }

    /**
     * Do edit roomlimit 
     * 
     * @param site
     * @return
     */
    @RequestMapping(value = "/{siteId}/edit-room-description", method = RequestMethod.PATCH)
    public @ResponseBody String onEditRoomDescription(@Preloading("siteId") Site site){
        
        Result result = new Result();
     
        try{
            
            siteService.edit(site);
            
            result.success().message("修改区域描述成功");
         
        }catch(Exception e){
            logger.error("onEditRoomDescription() - ", e); 
            result.fail().message("修改区域描述失败");
             
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display edit business hour panel
     * 
     * @param siteId
     * @return
     */
    @RequestMapping(value = "/{siteId}/edit-business-hour", method = RequestMethod.GET)
    public ModelAndView editBusinessHour(@PathVariable("siteId") Long siteId){
    
        ModelAndView modelAndView = new ModelAndView("system/site/site-edit-business-hour");
        
        Site site = siteService.load(siteId);

        modelAndView.addObject(site);
        
        return modelAndView;
        
    }

    /**
     * Do edit business hour 
     * 
     * @param site
     * @return
     */
    @RequestMapping(value = "/{siteId}/edit-business-hour", method = RequestMethod.PATCH)
    public @ResponseBody String onEditBusinessHour(@Preloading("siteId") Site site){
        
        Result result = new Result();
     
        try{
            
            siteService.edit(site);
            
            result.success().message("修改营业时间成功");
         
        }catch(Exception e){
            logger.error("onEditBusinessHour() - ", e); 
            result.fail().message("修改营业时间失败");
             
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 添加门店
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(){
        
        ModelAndView modelAndView = new ModelAndView("system/site/site-create");
        
        return modelAndView;
        
    }
    
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(Site site,
                                         @RequestParam("managerName") String managerName,
                                         @RequestParam("managerMobile") String managerMobile){
        
        Result result = new Result();
     
        try{
            if(StringUtils.isBlank(site.getName())) {
                result.fail().message("门店名称不能为空");
            } else if(StringUtils.isBlank(site.getAlias())) {
                result.fail().message("门店别名不能为空");
            } else if(StringUtils.isBlank(site.getCode())) {
                result.fail().message("门店编号不能为空");
            } else if(StringUtils.isBlank(site.getCity())) {
                result.fail().message("市不能为空");
            } else if(StringUtils.isBlank(site.getDistrict())) {
                result.fail().message("区不能为空");
            } else if(StringUtils.isBlank(site.getLocation().getAddress())) {
                result.fail().message("详细地址不能为空");
            } else if(StringUtils.isBlank(managerName)) {
                result.fail().message("店长账号不能为空");
            } else if(StringUtils.isBlank(managerMobile)) {
                result.fail().message("店长手机号不能为空");
            } else {
                siteService.create(site.trim(), managerName.trim(), managerMobile.trim());
                
                result.success().message("添加门店成功");
            }
        } catch(BusinessException e) {
            result.fail().message(e.getMessage());
        }catch(Exception e){
            logger.error("onCreateSite() - ", e); 
            result.fail().message("添加门店失败");
             
        }
        
        return JsonUtils.toJsonString(result);
    }
}
